<?php

namespace app\admin\model\scanwork;

use think\Model;

class WorkCenter extends Model
{
    protected $name = 'scanwork_work_center';
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function equipments()
    {
        return $this->hasMany('Equipment', 'work_center_id', 'id');
    }

    public static function generateCenterCode()
    {
        $prefix = 'WC-';
        $lastCode = self::order('id', 'desc')->value('center_code');
        if ($lastCode) {
            $lastNum = intval(substr($lastCode, -3));
            $newNum = str_pad($lastNum + 1, 3, '0', STR_PAD_LEFT);
        } else {
            $newNum = '001';
        }
        return $prefix . $newNum;
    }
}

